/*
 * @Date: 2023-09-14 17:06:22
 * @LastEditors: bb_c_e_f 18434367522@163.com
 * @LastEditTime: 2023-09-14 17:06:25
 * @FilePath: \part-cloud-appd:\project\company\veteran-cadre-app\src\util\uni.ts
 */
export function getLocation(isForce = false): Promise<any> {
  return new Promise((resolve) => {
    uni.getLocation({
      type: 'gcj02',
      success: (res) => {
        resolve(res)
      },
      fail: () => {
        if (isForce) {
          uni.getSetting({
            success(res) {
              if (
                res.authSetting['scope.userLocation'] === undefined
              || res.authSetting['scope.userLocation'] === false
              ) {
                uni.showModal({
                  title: '提示',
                  content: '您拒绝了定位权限，将可能导致距离不准，是否前往授权？',
                  success: (res2) => {
                    if (res2.confirm) {
                      uni.openSetting({
                        success: (res) => {
                          if (res.authSetting['scope.userLocation']) {
                            uni.getLocation({
                              type: 'gcj02',
                              success: (res) => {
                                resolve(res)
                              },
                              fail: (err) => {
                                console.error('获取位置信息失败', err)
                              },
                            })
                          }
                          else {
                          // 没有允许定位权限
                            uni.showToast({
                              title: '没有允许定位权限',
                              icon: 'none',
                            })
                          }
                        },
                      })
                    }
                  },
                })
              }
            },
          })
        }
      },
    })
  })
}
